/* eslint-disable */
import express from 'express';
import path from 'path';
import proxy from 'http-proxy-middleware';

export default class Server {
    constructor(port) {
        this._port = port;
        this._server = express();
    }

    init(dir, options) {
        this._server.use(express.static(dir, { dotfiles: 'allow' }));
        this._server.get('*', (_req, res) => res.sendFile(path.join(dir, 'index.html')));

        if (options?.server?.proxy) {
            for (let proxyPath of Object.keys(options.server.proxy)) {
                this._server.use(proxyPath, proxy(options.server.proxy[proxyPath]))
            }
        }

        return new Promise(resolve => {
            this._instance = this._server.listen(this._port, () => resolve());
        });
    }

    async destroy() {
        await this._instance?.close();
    }
}
